<?php

class CpusersController extends Core_Controller_Action 
{
	private $_users;
	
	public function init() {
	
		$this->_users = new Users();
	}
	
	public function indexAction() {
		$this->_forward( 'list', 'cpusers' );
	}
	
	public function itemAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$type = intval( $this->_request->getParam( 'type', 0 ) );
		
		$this->view->langslist = array();
		$Langs = new Langs();
		foreach( $Langs->getList()->toArray() as $value ) {
			$this->view->langslist[$value['l_id']] = $value['name'];
		}

		$Manufacturers = new Manufacturers();
		$this->view->manufacturers = $Manufacturers->getList()->toArray();
		
		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();
			
			foreach( $post as $key => $value ) {
				if( $key != 'pass' ) {
					$this->view->{$key} = $value;
				}
			}
			
			$validator = new Zend_Validate_EmailAddress();
			if( empty( $post['email'] ) || !$validator->isValid( $post['email'] ) ) {
				$this->view->err_email = 'Podaj poprawny email';
			} else {

				if( $id ) {
					$item = $this->_users->getItem( $id )->getFirst();

					foreach( $post as $key => $value ) {
	
						if( $key != 'rank' ) {
							
							if( $key == 'pass' ) {
								if( strlen( $value ) > 0 ) {
									$item->{$key} = md5( $value );
								}
							} else {
								$item->{$key} = $value;
							}
							//echo $key . ' => ' . $value.'<br />';
						}
					}

					try	{
						$item->save();
	
						$ManufacturersHasUsers = new ManufacturersHasUsers();
						$ManufacturersHasUsers->deleteByUser( $id );
						if( count( $post['rank'] ) ) {
							foreach( $post['rank'] as $key => $value ) {
								$ManufacturersHasUsers = new ManufacturersHasUsers();
								$ManufacturersHasUsers->manufacturers_m_id = $key;
								$ManufacturersHasUsers->users_u_id = $id;
								$ManufacturersHasUsers->rank_id = $value;
								$ManufacturersHasUsers->save();
							}
						}
	
						$this->_redirect( 'cpusers/list/type/' . $type . '/oper/2' );
					
					} catch( Exception $e ) {
						$this->view->err_email = 'Taki email już istnieje w bazie';
						$this->render( 'item' );
					}
				} else {
					foreach( $post as $key => $value ) {
	
						if( $key != 'rank' ) {
							if( ( $key == 'pass' ) ) {
								if( strlen( $value ) > 0 ) {
									$this->_users->{$key} = md5( $value );
								}
							} else {
								$this->_users->{$key} = $value;
							}
						}
					}

					$this->_users->registration_date = Date( 'Y:m:d H:i:s' );
					try	{
						$this->_users->save();

						if( $this->_users->u_id && count( $post['rank'] ) ) {
							foreach( $post['rank'] as $key => $value ) {
								$ManufacturersHasUsers = new ManufacturersHasUsers();
								$ManufacturersHasUsers->manufacturers_m_id = $key;
								$ManufacturersHasUsers->users_u_id = $this->_users->u_id;
								$ManufacturersHasUsers->rank_id = $value;
								$ManufacturersHasUsers->save();
							}
						}
	
						$this->_redirect( 'cpusers/list/type/' . $type . '/oper/1' );
					} catch( Exception $e ) {
						$this->view->err_email = 'Taki email już istnieje w bazie';
					}
				}
			}

		} else {
			if( $id ) {
				$item = $this->_users->getItem( $id )->toArray();
				
				$this->view->rank = array();
				
				$ManufacturersHasUsers = new ManufacturersHasUsers();
				foreach( $ManufacturersHasUsers->getByUser( $id )->toArray() as $value ) {
					$this->view->rank[$value['manufacturers_m_id']] = $value['rank_id'];
				}

				foreach( $item[0] as $key => $value ) {
					$this->view->{$key} = $value;
				}
			}
		
		}
		
		$this->assignType( $type );
	}
	
	public function listAction() {
		$type = intval( $this->_request->getParam( 'type', 1 ) );
		$page = intval( $this->_request->getParam( 'page', 1 ) );

		$pagerLayout = $this->_users->getListWithPager( $type, $page, $this->view->baseUrl() );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
				
		$this->assignType( $type );
	}
	
	public function deleteAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$type = intval( $this->_request->getParam( 'type', 0 ) );
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$item = $this->_users->getItem( $id )->getFirst();
			$item->state = 1;
			$item->save();
			
			$this->_redirect( 'cpusers/list/type/' . $type . '/oper/4' );
		} else {
			$this->_redirect( 'cpusers/list/type/' . $type . '/oper/3' );
		}
	}
	
	public function invitationAction() {
		$id = intval( $this->_getParam( 'id' ) );
		$page = intval( $this->_getParam( 'page' ) );
		
		$limit = 12;
		if( isSet( $post['excel'] ) && $post['excel'] ) {
			$limit = 10000;
		}
		
		$Invitation = new Invitation();
		
		if( $this->_request->isPost() ) {
			$post = $this->_request->getPost();
			
			if( $post['from'] || $post['to'] ) {
				$Users = new Users();
				
				$post['from'] = empty( $post['from'] ) ? false : $post['from'];
				$post['to'] = empty( $post['to'] ) ? false : $post['to'];
				
				$this->view->from = $post['from'];
				$this->view->to = $post['to'];
				
				$pagerLayout = $Invitation->getListWithPager( $id ,$page, $this->view->baseUrl(), $post['from'], $post['to'], $limit );
			} else {
				$pagerLayout = $Invitation->getListWithPager( $id ,$page, $this->view->baseUrl(), $limit );
			}
		} else {
			$pagerLayout = $Invitation->getListWithPager( $id ,$page, $this->view->baseUrl(), $limit );
		}

		$this->view->list = $pagerLayout->execute();
		
		if( isSet( $post['excel'] ) && $post['excel'] ) {
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNoRender();

			echo 'Użytkownik;Zaproszenie wysłano;Odpowiedział rejestracją' . "\n";

			foreach( $this->view->list as $item ) {
				$userTable = Doctrine_Core::getTable('Users');
				$el = $userTable->findOneByEmail( $item['email'] );
				if( $el ) {
					$us = 'Tak, dnia: ' . $el->registration_date;
				} else {
					$us = 'Nie';
				}
			
				echo $item['email'] . ';' . $item['add_date']  . ';' . $us . "\n";
			}

			$this->getResponse()->setHeader( 'Content-type', 'application/txt' );
			$this->getResponse()->setHeader( 'Content-Disposition', 'attachment; filename="invitation.csv"' );
		}
		$this->view->pager = $pagerLayout;
	}
	
	public function raportAction() {
		$Invitation = new Invitation();
		$id = intval( $this->_getParam( 'id' ) );
		
		$Users = new Users();
		$this->view->raportList = $Users->salesReport2( $id );
		
		if( $this->_request->isPost() ) {
			$post = $this->_request->getPost();
		
			$Users = new Users();
			
			$post['from'] = empty( $post['from'] ) ? false : $post['from'];
			$post['to'] = empty( $post['to'] ) ? false : $post['to'];
			
			$this->view->from = $post['from'];
			$this->view->to = $post['to'];
			
			$this->view->raportList = $Users->salesReport( $id, $post['from'], $post['to'] );
			
			if( isSet( $post['excel'] ) && $post['excel'] ) {
				$this->_helper->layout->disableLayout();
				$this->_helper->viewRenderer->setNoRender();

				echo 'Nr_transkacji;Klient;Data;Kwota_prowizji' . "\n";
				
				foreach( $this->view->raportList as $item ) {	
					$user = $item['Users']['name'] . ' ' . $item['Users']['surname'] . ' ' . $item['Users']['email'];
					
					$provision = $item['provision'];
					$provision = number_format( $provision, 2, '.', ' ' ) . ' ' . $item['currency'];
					echo $item['o_id'] . ';' . $user . ';' . $item['add_date']  . ';' . $provision . "\n";
					
				}
	
				$this->getResponse()->setHeader( 'Content-type', 'application/txt' );
				$this->getResponse()->setHeader( 'Content-Disposition', 'attachment; filename="provision_agentId_'. $id .'.csv"' );
			}
		}
	}
	
	public function assignType( $type ) {
		
		$this->view->type = $type;
		
		switch( $type ) {
			case 1 :
				$this->view->addText = 'Użytkownika';
				$this->view->yih2 = 'Użytkownicy';
				break;
			case 2 :
				$this->view->addText = 'Agenta';
				$this->view->yih2 = 'Agenci';
				break;
			case 3 :
				$this->view->addText = 'Administratora';
				$this->view->yih2 = 'Administratorzy';
				break;

		}
	}
}